package com.xh.load.controller;

import com.xh.load.service.UploadService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.commons.CommonsMultipartFile;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;

@Controller
public class ThumbnailController {

	@Autowired
	public UploadService uploadService;

	@RequestMapping("/index")
	public String index(){
		return "index";
	}

	@RequestMapping("/picupload")
	public String pic_upload(){
		return "pic_upload";
	}

	/**
	 * 图片上传并处理
	 * @param file
	 * @param session
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/thumbnail", method = RequestMethod.POST)
	public ModelAndView thumbnail(@RequestParam("image") CommonsMultipartFile file, HttpSession session){
		String uploadPath = "/images";
		String realUploadPath = session.getServletContext().getRealPath(uploadPath);
		String imageUrl = uploadService.uploadImage(file, uploadPath, realUploadPath);
		String thum4ImageUrl = uploadService.uploadThum4Image(file, uploadPath, realUploadPath);
		ModelAndView mav = new ModelAndView();
		mav.addObject("imagesUrl", imageUrl);
		mav.addObject("thumbnailUrl", thum4ImageUrl);
		mav.setViewName("thumbnail");
		return mav;
	}
	
	/**
	 * AJAX上传图片并处理
	 * @param file
	 * @param session
	 * @return
	 */
	@RequestMapping(value = "/upload", method = RequestMethod.POST)
	public @ResponseBody Object upload(@RequestParam("image") CommonsMultipartFile file, HttpSession session){
		String uploadPath = "/images";
		String realUploadPath = session.getServletContext().getRealPath(uploadPath);
		String thum4ImageUrl = uploadService.uploadThum4Image(file, uploadPath, realUploadPath);
		return thum4ImageUrl;
	}

}